package xyz.zhzby.cyzx.oj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import xyz.zhzby.cyzx.oj.dto.request.CheckPointAddRequest;
import xyz.zhzby.cyzx.oj.dto.response.CheckPointInfo;
import xyz.zhzby.cyzx.oj.entity.Checkpoint;
import xyz.zhzby.cyzx.oj.mapper.CheckpointMapper;
import xyz.zhzby.cyzx.oj.service.ICheckpointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhzby.cyzx.security.entity.User;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 测试点 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-12-26
 */
@Service
public class CheckpointServiceImpl extends ServiceImpl<CheckpointMapper, Checkpoint> implements ICheckpointService {

    @Override
    public void linkProblemCheckpoint(Long id, List<CheckPointAddRequest> checkPoints) {
        removeByProblemId(id);

        User subject = CyzxSecurityContextHolder.getSubject();
        for (int i = 0; i < checkPoints.size(); i++) {
            Checkpoint checkpoint = new Checkpoint();
            checkpoint.setProblemId(id);
            checkpoint.setNumber(i + 1);
            CheckPointAddRequest checkPointAddRequest = checkPoints.get(i);
            checkpoint.setInput(checkPointAddRequest.getInput());
            checkpoint.setOutput(checkPointAddRequest.getOutput());
            checkpoint.setScore(checkPointAddRequest.getScore());
            subject.fillCreateInfo(checkpoint, DataLogInfo.USER_FIELD_ACTUAL_NAME);
            baseMapper.insert(checkpoint);
        }
    }

    @ApiOperation("根据试题ID删除关联")
    private void removeByProblemId(Long problemId) {
        LambdaQueryWrapper<Checkpoint> qw = new LambdaQueryWrapper<>();
        qw.eq(Checkpoint::getProblemId, problemId);
        baseMapper.delete(qw);
    }

    @Override
    public List<CheckPointInfo> getLinkCheckpointsByProblemID(Long problemID) {
        LambdaQueryWrapper<Checkpoint> qw = new LambdaQueryWrapper<>();
        qw.eq(Checkpoint::getProblemId, problemID);
        List<Checkpoint> checkpoints = baseMapper.selectList(qw);
        List<CheckPointInfo> ans = new ArrayList<>();
        for (Checkpoint checkpoint : checkpoints) {
            CheckPointInfo checkPointInfo = new CheckPointInfo();
            checkPointInfo.setEdit(false);
            checkPointInfo.setInput(checkpoint.getInput());
            checkPointInfo.setOutput(checkpoint.getOutput());
            checkPointInfo.setScore(checkpoint.getScore());
            checkPointInfo.setKey(checkpoint.getId());
            ans.add(checkPointInfo);
        }
        return ans;
    }

    @Override
    public int getNumById(Long checkpointId) {
        LambdaQueryWrapper<Checkpoint> qw = new LambdaQueryWrapper<>();
        qw.eq(Checkpoint::getId, checkpointId);
        qw.select(Checkpoint::getNumber);
        Checkpoint checkpoint = baseMapper.selectOne(qw);
        return checkpoint.getNumber();
    }

}
